package com.ea.controller.back;

import com.ea.api.controller.admin.OrderManageApi;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.common.util.TypeConversionToJson;
import com.ea.domain.dto.UpdateMemoDTO;
import com.ea.domain.entity.OrderFormDetail;
import com.ea.domain.vo.OrderDetailVO;
import com.ea.domain.query.OrderManageQueryDTO;
import com.ea.domain.dto.OrderManageUpdateDTO;
import com.ea.domain.entity.OrderFormLog;
import com.ea.service.IOrderLogService;
import com.ea.service.impl.IOrderManageInfoServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @author NAME-L
 * @Description 后台订单管理
 * @className OrderManageController
 * @date 2022-12-13 21:39:48
 */
@RestController
@RequestMapping("/electric/back/order/manage")
public class OrderManageController implements OrderManageApi {

    @Autowired
    private IOrderManageInfoServiceImpl orderFormInfoService;

    @Autowired
    private IOrderLogService logService;


    @Override
    @PostMapping("/get/info/list")
    public JsonResult getOrderInfoList(@RequestBody OrderManageQueryDTO queryDTO) {
        return JsonResult.doSuccessful(orderFormInfoService.selectListByCondition(queryDTO));
    }

    @Override
    @PostMapping("/update/info")
    public JsonResult updateOrderInfo(@RequestBody OrderManageUpdateDTO updateDTO) {
        if (updateDTO == null || updateDTO.getOrderId() == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        orderFormInfoService.updateOrderInfo(updateDTO);
        return JsonResult.updateSuccessFul();
    }

    @Override
    @PostMapping("/add/memo")
    public JsonResult addOrderMemo(@RequestBody UpdateMemoDTO memoDTO) {
        return JsonResult.doSuccessful(orderFormInfoService.addOrderInfoMemo(memoDTO));
    }

    @Override
    @GetMapping("/confirm")
    public JsonResult confirmOrderInfoList(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        orderFormInfoService.confirmOrderInfoList(Collections.singletonList(orderId));
        return JsonResult.updateSuccessFul();
    }

    @Override
    @GetMapping("/deliver")
    public JsonResult deliverOrderInfo( @RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        orderFormInfoService.deliverOrderInfoList(Collections.singletonList(orderId));
        return null;
    }

    @Override
    @GetMapping("/get/info/detail")
    public JsonResult getOrderInfoDetail(@RequestParam("orderId")Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        List<OrderFormDetail> list = orderFormInfoService.selectOrderDetailByOrderId(orderId);
        return JsonResult.doSuccessful(list);
    }

    @Override
    @GetMapping("/get/log")
    public JsonResult getOrderInfoLog(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        List<OrderFormLog> orderLogList = logService.selectOrderLogByOrderId(orderId);
        return JsonResult.doSuccessful(orderLogList);
    }
}
